//
//  TCMyMessagesViewController.m
//  陶瓷
//
//  Created by 王戈 on 15/8/3.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "TCMyMessagesViewController.h"
#import "TCMyMessageCell.h"

#import "DiscoverJobViewController.h"
#import "DiscoverShopViewController.h"
#import "DiscoverMarketViewController.h"
#import "TailDetailViewController.h"

#import "MJRefresh.h"

typedef NS_ENUM(NSInteger, RefreshType) {
    PullUpRefresh,
    PushDownRefresh,
};

@interface TCMyMessagesViewController ()

@property (nonatomic, strong) NSMutableArray *messages;
@property (nonatomic, assign) int pn;
@property (nonatomic, strong) UIButton *cover;

@end

@implementation TCMyMessagesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.pn = 1;
    
    __weak UITableView *tableView = self.tableView;
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pn = 1;
        [self refreshMessagesWithType:PushDownRefresh];
    }];
    tableView.header.autoChangeAlpha = YES;
    tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pn += 1;
        [self refreshMessagesWithType:PullUpRefresh];
    }];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self.navigationItem setTitle:@"我的信息"];
    [self refreshMessagesWithType:PushDownRefresh];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationItem setTitle:@""];
}

- (void)refreshMessagesWithType:(RefreshType)type {
    [SVProgressHUD showWithStatus:@"正在请求信息数据 请稍后.."];
    [self.view addSubview:self.cover];
    if (type == PushDownRefresh) {
        // 获得发布信息列表
        [[TCNetworkTool sharedNetTool] messageListWithPn:[NSString stringWithFormat:@"%d",self.pn] andSuccessBlocks:^(NSArray *resultArray) {
            if ([_messages count] > 0) {
                [self.messages removeAllObjects];
            }
            _messages = [resultArray mutableCopy];
            [SVProgressHUD dismiss];
            [self.tableView.header endRefreshing];
            [self.cover removeFromSuperview];
            [self.tableView reloadData];
        } andFailureBlocks:^(NSString *failureMessage) {
            [SVProgressHUD dismiss];
            [self.tableView.header endRefreshing];
            [MBHUDHelper showWarningWithText:failureMessage];
            [self.cover removeFromSuperview];
        }];
    }
    else
    {
        // 获得发布信息列表
        [[TCNetworkTool sharedNetTool] messageListWithPn:[NSString stringWithFormat:@"%d",self.pn] andSuccessBlocks:^(NSArray *resultArray) {
            [_messages addObjectsFromArray:resultArray];
            [SVProgressHUD dismiss];
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
            [self.cover removeFromSuperview];
        } andFailureBlocks:^(NSString *failureMessage) {
            [SVProgressHUD dismiss];
            [self.tableView.footer endRefreshing];
            [STAlertView st_promptAlertWithMessage:@"获取信息失败 ,请检查网络后重试!"];
            [self.cover removeFromSuperview];
        }];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TCMyMessageCell *cell = [TCMyMessageCell cellWithTableView:tableView];
    if (indexPath.row > [_messages count]-1) {
        return cell;
    }
    cell.message = _messages[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _messages.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TCMyMessageCell *cell = (TCMyMessageCell *)[tableView cellForRowAtIndexPath:indexPath];
    switch (cell.type) {
        case kTCMessageTypeRecruitment:
        {
            DiscoverJobViewController *jb = [[DiscoverJobViewController alloc] init];
            jb.sid = cell.message.Id;
            jb.mine = YES;
            [self.navigationController pushViewController:jb animated:YES];
        }
            break;
        case kTCMessageTypeRent:
        {
        DiscoverShopViewController *jb = [[DiscoverShopViewController alloc] init];
            jb.sid = cell.message.Id;
            jb.mine = YES;
            
            [self.navigationController pushViewController:jb animated:YES];
        
        }
            break;
        case kTCMessageTypeTailGoods:
        {
            TailDetailViewController *tailVC = [[TailDetailViewController alloc] init];
            tailVC.proid = cell.message.Id;
            tailVC.mine = YES;
            [self.navigationController pushViewController:tailVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [SVProgressHUD showWithStatus:@"正在请求删除 请稍后.."];
        [self.view addSubview:self.cover];
        TCMyMessageCell *cell = (TCMyMessageCell *)[tableView cellForRowAtIndexPath:indexPath];
        switch (cell.type) {
            case kTCMessageTypeRecruitment:
            {
                [[TCNetworkTool sharedNetTool] RecruitmentDeleteWithID:cell.message.Id andSuccessBlocks:^(NSString *successMessage) {
                    [self deleteSuccessWithIndexPath:indexPath andTableView:tableView];
                } andFailureBlocks:^(NSString *failureMessage) {
                    [self deleteFailureWithMessage:failureMessage];
                }];
            }
                break;
            case kTCMessageTypeRent:
            {
                [[TCNetworkTool sharedNetTool] rentDeleteWithID:cell.message.Id andSuccessBlocks:^(NSString *successMessage) {
                    [self deleteSuccessWithIndexPath:indexPath andTableView:tableView];
                } andFailureBlocks:^(NSString *failureMessage) {
                    [self deleteFailureWithMessage:failureMessage];
                }];
            }
                break;
            case kTCMessageTypeTailGoods:
            {
                [[TCNetworkTool sharedNetTool] tailGoodsDeleteWithID:cell.message.Id andSuccessBlocks:^(NSString *successMessage) {
                    [self deleteSuccessWithIndexPath:indexPath andTableView:tableView];
                } andFailureBlocks:^(NSString *failureMessage) {
                    [self deleteFailureWithMessage:failureMessage];
                }];
            }
                break;
            default:
                break;
        }
    }
}

- (void)deleteSuccessWithIndexPath:(NSIndexPath *)indexPath andTableView:(UITableView *)tableView {
    
    [SVProgressHUD dismiss];
    [SVProgressHUD showSuccessWithStatus:@"删除成功!"];
    [self.cover removeFromSuperview];
    
    [self.messages removeObjectAtIndex:[indexPath row]];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
}

- (void)deleteFailureWithMessage:(NSString *)message {
    [SVProgressHUD dismiss];
    [self.cover removeFromSuperview];
    [MBHUDHelper showWarningWithText:message];
}
@end
